import sys
import pygame
from pygame.locals import *
from config import *
from game_manager import GameManager


def main():
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption('扫雷')

    # 游戏管理器
    game_manager = GameManager(screen)
    print(game_manager)

    # 保存鼠标点击信息
    mouse_data = {}

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()

            elif event.type == MOUSEBUTTONDOWN:
                # 清空鼠标点击信息
                mouse_data = {}

                # 保存鼠标点击信息
                mouse_data["x"] = event.pos[0] // SIZE         # 逻辑x坐标
                mouse_data["y"] = event.pos[1] // SIZE - 2     # 逻辑y坐标
                mouse_data["key"] = pygame.mouse.get_pressed() # 鼠标左键点击1 0 0 中键点击0 1 0 右键点击0 0 1

            elif event.type == MOUSEBUTTONUP:
                x = mouse_data["x"]
                y = mouse_data["y"]
                if 0 <= x < BLOCK_WIDTH and 0 <= y < BLOCK_HEIGHT: # 检测点击在有效区域内
                    if mouse_data["key"][0]:
                        # 鼠标左键点击相关处理
                        game_manager.left_click(x, y)
                    elif mouse_data["key"][2]:
                        # 鼠标右键点击相关处理
                        game_manager.right_click(x, y)

        game_manager.draw_mine_map()

        pygame.display.update()


if __name__ == '__main__':
    main()

